import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {
    private static User user;

    public static void main(String[] args) {
        Entity entity = new Entity();
//        entity.executeQuery();
//        entity.getAvailableTimes();
//        entity.queryIntervalsByDate(new Date(2021 - 1900, Calendar.DECEMBER, 10));
        Scanner sc = new Scanner(new BufferedInputStream(System.in));

        System.out.println("欢迎使用体育馆预约系统");

        user = new User();
        while (!user.loggedIn) {
            System.out.println("请登录");
            System.out.print("请输入账号: ");
            String username = sc.nextLine();
            if (!user.has(username)) {
                System.out.println("用户不存在！");
                continue;
//                System.exit(-1);
            }
            for (int count = 0; !user.loggedIn; ) {
                System.out.print("请输入密码: ");
                String password = sc.nextLine();
                if (!user.login(username, password)) {
                    System.out.println("密码错误！");
                    count++;
                    if (count == 3) {
                        System.out.println("连续三次密码输入错误！");
                        System.exit(-1);
                    }
                } else break;
            }
        }
        System.out.println("成功登录");

        System.out.print("""
                请选择项目：
                1. 查看今日可预约时间
                2. 预约
                3. 取消预约
                4. 退出
                """);
        outer:
        while (true) {
            System.out.print("请输入您的选择: ");
            try {
                int choice = sc.nextInt();
                switch (choice) {
                    case 1:
                        entity.executeQuery();
                        break;
                    case 2:
                        System.out.println("请输入要预约时间段的ID");
                        int idToBook = sc.nextInt();
                        entity.tryBook(idToBook, user.id);
                        break;
                    case 3:
                        System.out.println("请输入要取消预约的时间段ID");
                        int idToUnbook = sc.nextInt();
                        entity.tryUnbook(idToUnbook, user.id);
                        break;
                    default:
                        break outer;
                }
            } catch (java.util.InputMismatchException e) {
                break outer;
            }
        }
        System.out.println("Bye~~");
        entity.close();
    }
}
